<?php
namespace app\manage\controller;
use  think\facade\Db;
class Project extends Base
{

    public function typeList() {
        $list = [
            ['id'=>1,'name'=>'冷链物流项目'],
            ['id'=>2,'name'=>'物流基础设施项目'],
            ['id'=>3,'name'=>'绿色智慧物流项目'],
            ['id'=>4,'name'=>'城乡物流体系项目'],
            ['id'=>5,'name'=>'工业物流项目'],
        ];
        return ajax($list);
    }

    public function tableList()
    {
        $area = input('post.area','');
        $project_type = input('post.project_type','');
        $year = input('post.year','');
        $season = input('post.season','');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $where = [
            ['del','=',0]
        ];
        if($area) {
            $where[] = ['area','=',$area];
        }
        if($project_type) {
            $where[] = ['project_type','=',$project_type];
        }
        if($year) {
            $where[] = ['year','=',$year];
        }
        if($season) {
            $where[] = ['season','=',$season];
        }
        try {
            $count = Db::table('project')->where($where)->count();
            $list = Db::table('project')->where($where)
                ->order(['year'=>'ASC','season'=>'DESC','id'=>'DESC'])
                ->limit(($curr_page - 1)*$perpage,$perpage)->select();
        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }

    public function itemDetail() {
        $post['id'] = input('post.id');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('project')->where($where)->find();
            if(!$exist) {
                return ajax('',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($exist);
    }

    public function saveData() {
        $post['company'] = input('post.company','');
        $post['project_name'] = input('post.project_name','');
        $post['project_type'] = input('post.project_type','');
        $post['area'] = input('post.area','');
        $post['status'] = input('post.status','');
        $post['year'] = input('post.year','');
        $post['season'] = input('post.season','');
        checkPost($post);
        $post['desc'] = input('post.desc','');
        $post['id'] = input('post.id','');
        try {
            if($post['id']) {
                Db::table('project')->where('id','=',$post['id'])->update($post);
            }else {
//                $where = [
//                    ['year','=',$post['year']],
//                    ['trade','=',$post['trade']],
//                    ['area','=',$post['area']],
//                ];
//                $exist = Db::table('project')->where($where)->find();
//                if($exist) {
//                    return ajax('',-1,'数据已存在!不可重复添加');
//                }
                Db::table('project')->insertGetId($post);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

    public function itemDelete() {
        $post['id'] = input('post.id');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('project')->where($where)->find();
            if(!$exist) {
                return ajax('',-4);
            }
            Db::table('project')->where($where)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($exist);
    }


}